package com.example.wifiunlockscreen.support;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Comment;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.example.wifiunlockscreen.database.WifiFriendHelper;

/***CLASE QUE SE CONECTARA CON LA BBDD***/

public class WifiFriendDAO {
	
	  // Database fields
	  private SQLiteDatabase database;
	  private WifiFriendHelper dbHelper;
	  
	  private String[] allColumns = { WifiFriendHelper.COLUMN_ID, WifiFriendHelper.COLUMN_NAME, WifiFriendHelper.COLUMN_FAVORITE };

	  
	  public WifiFriendDAO(Context context) {
	    dbHelper = new WifiFriendHelper(context);
	  }

	  public void open() throws SQLException {
	    database = dbHelper.getWritableDatabase();
	  }

	  public void close() {
	    dbHelper.close();
	  }
	  
	  
	  

	  public Boolean saveFriend(String name) {
		  
	    ContentValues values = new ContentValues();//comentario guay
	    values.put(WifiFriendHelper.COLUMN_NAME, name);
	    values.put(WifiFriendHelper.COLUMN_FAVORITE, 1);
	    
	    long insertId = database.insert(WifiFriendHelper.TABLE_FRIENDS, null, values);//otro comentario
	    
	    Cursor cursor = database.query(
	    		WifiFriendHelper.TABLE_FRIENDS, allColumns, WifiFriendHelper.COLUMN_ID + " = " + insertId, null, null, null, null);
	    
	    cursor.moveToFirst();
	    
	    WifiFriendObject wifiFriend = cursorToWifiFriend(cursor);
	    cursor.close();
	    
	    if(!wifiFriend.getNombre().equals("")){
	    	return true;
	    }
	    
	    return false;
	  }

	  public Integer deleteFriend(WifiFriendObject wifiFriend) {
	    long id = wifiFriend.getId();
	    System.out.println("WifiFriendObject deleted with id: " + id);
	    
	    if(wifiFriend!=null && wifiFriend.getId()!=null){
	    	return database.delete(WifiFriendHelper.TABLE_FRIENDS, WifiFriendHelper.COLUMN_ID + " = " + id, null);
	    }else{
	    	return 0;
	    }
	    
	    
	  }

	  public List<WifiFriendObject> getAllFriends() {
	    List<WifiFriendObject> wifiFriendList = new ArrayList<WifiFriendObject>();

	    Cursor cursor = database.query(WifiFriendHelper.TABLE_FRIENDS, allColumns, null, null, null, null, null);

	    cursor.moveToFirst();
	    
	    while (!cursor.isAfterLast()) {
	    	WifiFriendObject wifiFriend = cursorToWifiFriend(cursor);
	    	wifiFriendList.add(wifiFriend);
	    	cursor.moveToNext();
	    }
	    // make sure to close the cursor
	    cursor.close();
	    return wifiFriendList;
	  }

	  private WifiFriendObject cursorToWifiFriend(Cursor cursor) {
		  
		WifiFriendObject wifiFriend = new WifiFriendObject();
		
		if(cursor!=null){
			
			wifiFriend.setId((int) cursor.getLong(0));
			wifiFriend.setNombre(cursor.getString(1));
		    return wifiFriend;
			
		}else{
			return null;
		}

	  }
	  
	  public WifiFriendObject getByName(String nombreWifi){
		  
		  WifiFriendObject wifiFriend = null;
		  
		  /*
		   * cursor = db.query(
		   * TABLE_NAME, 
		   * new String[] { TABLE_COLUMN_ID, TABLE_COLUMN_ONE, TABLE_COLUMN_TWO },
		   * TABLE_COLUMN_ONE + " LIKE ? AND " + TABLE_COLUMN_TWO + " LIKE ?",
		   * new String[] {"%" + dan + "%", "%" + vrijeme + "%",
			null, null, null, null);
		   * */
		  
		  if(nombreWifi!=null){
			  Cursor cursor = database.query(WifiFriendHelper.TABLE_FRIENDS, 
						allColumns, 
						WifiFriendHelper.COLUMN_NAME + " LIKE ? AND " + WifiFriendHelper.COLUMN_FAVORITE + " = 1",
						new String[] {nombreWifi},
						null, null, null, null);
			
			  
				if(cursor!=null)
				if(cursor.moveToFirst()){
					if(!cursor.isAfterLast()) {
						wifiFriend = cursorToWifiFriend(cursor);
					}
				}


	
			// make sure to close the cursor
			cursor.close();
		  }

		  
		  
		  return wifiFriend;
	  }
}
